package com.example.shopcardemo;
 
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * 增加和减少商品数量的View
 * @author Administrator
 *
 */
public class ChangNumberView extends LinearLayout{
	private Context ctx;
	private int currentNumber=0;
	private ImageView addIv,cutIv;
	private TextView currentTv;

	public ChangNumberView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ChangNumberView(Context context) {
		super(context);
		init();
	}
	
	private void init(){
		this.ctx=getContext();
		this.setOrientation(LinearLayout.HORIZONTAL);
		this.setGravity(Gravity.CENTER_VERTICAL);
		addIv=new ImageView(ctx);
		cutIv=new ImageView(ctx);
		currentTv=new TextView(ctx);
		
		addIv.setImageResource(R.drawable.good_add);
		cutIv.setImageResource(R.drawable.good_cut);
		LayoutParams alp=new LayoutParams(UiUtils.dip2px(30), UiUtils.dip2px(30));
		addIv.setLayoutParams(alp);
		LayoutParams clp=new LayoutParams(UiUtils.dip2px(30), UiUtils.dip2px(30));
		cutIv.setLayoutParams(clp);
		currentTv.setGravity(Gravity.CENTER);
		currentTv.setTextColor(Color.BLACK);
		currentTv.setPadding(10, 0, 10, 0);
		addIv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				currentNumber++;
				refreshView();
				
				if(changeListener!=null){
					changeListener.onChange(currentNumber);
				}
				
				if(addListener!=null){
					addListener.onAdd(currentNumber);
				}
			}
		});
		
		cutIv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(currentNumber>0){
					currentNumber--;
					refreshView();
					if(changeListener!=null){
						changeListener.onChange(currentNumber);
					}
					if(currentNumber==0){
						if(cutListener!=null){
							cutListener.onCutFinish();
						}
					}
					if(cutListener!=null){
						cutListener.onCut(currentNumber);
					}
				}
			}
		});
		
		refreshView();
		
		this.addView(cutIv);
		this.addView(currentTv);
		this.addView(addIv);
	}
	/**
	 * 刷新布局
	 */
	private void refreshView(){
		if(currentNumber<=0){
			cutIv.setVisibility(View.INVISIBLE);
			currentTv.setVisibility(View.INVISIBLE);
		}else{
			cutIv.setVisibility(View.VISIBLE);
			currentTv.setVisibility(View.VISIBLE);
			currentTv.setText(String.valueOf(currentNumber));
		}
	}
	/**
	 * 获得当前的数量
	 * @return
	 */
	public int getCurrentNumber(){
		return currentNumber;
	}
	/**
	 * 设置当前的数量
	 * @param currentNumber
	 */
	public void setCurrentNumber(int currentNumber){
		if(currentNumber>=0){
			this.currentNumber=currentNumber;
			refreshView();
		}
	}
	
	public int[] getWindowsLocation(){
		int[] location=new int[2];
		addIv.getLocationInWindow(location);
		return location;
	}
	
	
	//数量减少监听
	public interface CutNumberListener{
		void onCut(int currentNumber);
		void onCutFinish();
	}
	private CutNumberListener cutListener;
	public void setCutNumberListener(CutNumberListener cutListener){
		this.cutListener=cutListener;
	}
	//数量增加监听
	public interface AddNumberListener{
	    void onAdd(int currentNumber);
	}
	private AddNumberListener addListener;
	public void setAddNumberListener(AddNumberListener addListener){
		this.addListener=addListener;
	}
	//数量改变监听
	public interface ChangeNumberListener{
		void onChange(int currentNumber);
	}
	private ChangeNumberListener changeListener;
	public void setChangeNumberListener(ChangeNumberListener changeListener){
		this.changeListener=changeListener;
	}
	

}
